package br.edu.ufc.deti.util;
/*
 *  Copyright 2006 Corey Goldberg (corey@goldb.org)
 *
 *  This file is part of NetPlot.
 *
 *  NetPlot is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  NetPlot is distributed in the hope that it will be useful,
 *  but without any warranty; without even the implied warranty of
 *  merchantability or fitness for a particular purpose.  See the
 *  GNU General Public License for more details.
 */
import java.io.*;
import java.util.ArrayList;

import br.edu.ufc.deti.beans.Aplicacao;
import br.edu.ufc.deti.beans.Mensagem;
import br.edu.ufc.deti.beans.PerfClusters;
import br.edu.ufc.deti.beans.Recurso;

public class Utils {
    // run a system command and return output
    public static String cmdExec(String cmdLine) {
        String line;
        String output = "";
        try {
            Process p = Runtime.getRuntime().exec(cmdLine);
            BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
            while ((line = input.readLine()) != null) {
                output += (line);
            }
            input.close();
        } 
        catch (Exception ex) {
            ex.printStackTrace();
        }
        return output;
    }
	public String lerArquivo(String localArquivo){
		StringBuffer sb = new StringBuffer();
		File resultado = new File("/home/nimbus/workspace/perfmachines/aplicacoes/helloMPI_resultado.txt");
		try {
			FileReader reader = new FileReader(resultado);
			int c;
			do{
				c = reader.read();
				if(c!=-1){
					sb.append((char)c);
				}
			}while(c!=-1);
			reader.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		System.out.println(sb.toString());
		return sb.toString();
	}

	public static byte[] read(final File file) {
		if (file.isDirectory())
			throw new RuntimeException("Unsupported operation, file "+ file.getAbsolutePath() + " is a directory");
		if (file.length() > Integer.MAX_VALUE)
			throw new RuntimeException("Unsupported operation, file "+ file.getAbsolutePath() + " is too big");

		Throwable pending = null;
		FileInputStream in = null;
		final byte buffer[] = new byte[(int) file.length()];
		try {
			in = new FileInputStream(file);
			in.read(buffer);
		} catch (Exception e) {
			pending = new RuntimeException("Exception occured on reading file "+ file.getAbsolutePath(), e);
		} finally {
			if (in != null) {
				try {
					in.close();
				} catch (Exception e) {
					if (pending == null) {
						pending = new RuntimeException("Exception occured on closing file"+ file.getAbsolutePath(), e);
					}
				}
			}
			if (pending != null) {
				throw new RuntimeException(pending);
			}
		}
		return buffer;
	}
	
	public static PerfClusters populaPerfClusters(){
		PerfClusters perfClusters = new PerfClusters();
		perfClusters.setAplicacao(new Aplicacao());
		perfClusters.setRecursos(new ArrayList<Recurso>());
		perfClusters.setMensagens(new ArrayList<Mensagem>());
		return perfClusters;
	}
	
    public static void main(String[] args) {
        System.out.println(cmdExec("ls -a"));
    }
}